package com.gec.domain;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/*
  2. PageBean 对象
          设置相关属性:
	{a}int code;
	{b}String msg;
	{c}int count;
	{d}List<T> data;

	相关方法:
	a. toJSON();
 */
public class PageBean<T> {
    private int code = 0;
    private String msg = "";
    private int count = 0;
    private List<T> data = new ArrayList<>();

    public PageBean() {
    }

    public PageBean(int count) {
        this.count = count;
    }

    //相关方法:
    public String toJSON() {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code", code);
        jsObj.put("msg", msg);
        jsObj.put("count", count);
        jsObj.put("data", data);
        return jsObj.toJSONString();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public String errorMsg() {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code", -1);
        jsObj.put("msg", "");
        return jsObj.toJSONString();
    }

    public String errMsg(Exception e) {
        JSONObject jsObj = new JSONObject();
        jsObj.put("code",100);
        jsObj.put("msg", e.getMessage());
        return jsObj.toJSONString();
    }
}
